<?php
/**
 * @ClassName: Shopguaranteeservices
 * @Description: 保障服务
 * @author: gydtrade
 * @Date: 2024-09-09 22:16
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\enum\CommonEnum;
use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Shopguaranteeservices extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\GuaranteeServices();
        $this->pageTitle = '保障服务';
        $this->enableField = 'status';
        $this->pagesize = 10;

        $this->selectTextField = '{title}';
        $this->selectFields = 'id,title';
        $this->selectSearch = 'title';

        $this->indexWith = ['merchant', 'store'];
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        $search->text('title', '保障服务名称');
        $search->select('merchant_id', '商家')->default(url('admin/merchant.merchant/selectPage'));
        $search->select('store_id', '店铺')->default(url('admin/store.store/selectPage'));
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (!empty($searchData['title'])) {
            $where[] = ['title', 'like', '%' . $searchData['title'] . '%'];
        }
        if (isset($searchData['merchant_id']) && $searchData['merchant_id'] != '') {
            $where[] = ['merchant_id', 'eq', $searchData['merchant_id']];
        }
        if (isset($searchData['store_id']) && $searchData['store_id'] != '') {
            $where[] = ['store_id', 'eq', $searchData['store_id']];
        }
        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('title', '保障服务名称');
        $table->match('merchant_id', '商家')->to('{merchant_id}#{merchant.merchant_name}');
        $table->match('store_id', '店铺')->to('{store_id}#{store.store_name}');
        $table->show('content', '保障服务内容');
        $table->match('is_del', '状态')->options(CommonEnum::delText(true))->mapClassGroup([[0, 'success'], [1, 'danger']]);
        $table->switchBtn('is_show', '显示')->default(1)->autoPost();

        $table->fields('create_times', '添加/修改时间')->with(
            $table->show('create_time', '添加时间'),
            $table->show('update_time', '修改时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnDelete()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([], 'post');

        $result = $this->validate($data, []);

        if (!$result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}